﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LBIEater
{
    class Program
    {
        static Timer RegInfoTimer { get; set; }

        static Timer SessionTimer { get; set; }

        static int RegInfoTimerInterval { get; set; }

        static int SessionTimerInterval { get; set; }

        static void Main(string[] args)
        {
            Console.Title = "LBI 监控控制台";
            RegInfoTimerInterval = 1800000;
            SessionTimerInterval = 7200000;

            RegInfoTimer = new Timer(arg =>
            {
                using (OracleConnection c = new OracleConnection("DATA SOURCE=--;PERSIST SECURITY INFO=True;USER ID=--;PASSWORD=--"))
                {
                    c.Open();
                    using (OracleCommand cmd = new OracleCommand("delete from f_reportreginfo", c))
                    {
                        var count = cmd.ExecuteNonQuery();
                        Console.WriteLine();
                        Console.WriteLine(DateTime.Now + " | delete reginfo : " + count);
                    }
                }
            }, null, 60000, RegInfoTimerInterval);

            SessionTimer = new Timer(arg =>
            {
                string path = @"C:\Program Files (x86)\Kingdee_GI\LBIServer\FZReportServer.exe";
                ManagementObjectSearcher s = new ManagementObjectSearcher(@"select * from Win32_Process where name = ""FZReportServer.exe"""); //FZReportServer
                foreach (ManagementObject item in s.Get())
                {
                    item.InvokeMethod(new ManagementOperationObserver(), "Terminate", null);
                    //path = item["ExecuteablePath"].ToString();
                }
                Console.WriteLine();
                Console.WriteLine(DateTime.Now + " | Terminated : " + path);

                if (string.IsNullOrWhiteSpace(path))
                    return;

                ManagementClass processClass = new ManagementClass("Win32_Process");
                ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
                inParams["CommandLine"] = path;
                ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);

                Console.WriteLine(DateTime.Now + " | Created : " + path);
            }, null, 60000, SessionTimerInterval);

            string input = null;
            do
            {
                input = Console.ReadLine();
                switch (input)
                {
                    case "help":
                        Console.WriteLine(">显示所有命令:");
                        Console.WriteLine(">enum");
                        Console.WriteLine(">stop {Timer}");
                        Console.WriteLine(">set {Timer} {Interval}");
                        Console.WriteLine(">Exit");
                        break;
                    case "enum":
                        Console.WriteLine(">Name :\t\tRegInfoTimer\r\n>State :\t" + (RegInfoTimerInterval > 0) + "\r\n>Interval :\t" + RegInfoTimerInterval);
                        Console.WriteLine(">-------------------");
                        Console.WriteLine(">Name :\t\tSessionTimer\r\n>State :\t" + (SessionTimerInterval > 0) + "\r\n>Interval :\t" + SessionTimerInterval);
                        break;
                    case "stop RegInfoTimer":
                        RegInfoTimer.Change(Timeout.Infinite, Timeout.Infinite);
                        RegInfoTimerInterval = -1;
                        Console.WriteLine(">Name :\t\tRegInfoTimer\r\n>State :\t" + (RegInfoTimerInterval > 0) + "\r\n>Interval :\t" + RegInfoTimerInterval);
                        break;
                    case "stop SessionTimer":
                        SessionTimer.Change(Timeout.Infinite, Timeout.Infinite);
                        SessionTimerInterval = -1;
                        Console.WriteLine(">Name :\t\tSessionTimer\r\n>State :\t" + (SessionTimerInterval > 0) + "\r\n>Interval :\t" + SessionTimerInterval);
                        break;
                    case "set RegInfoTimer":
                        Console.Write("Interval:");
                        var ri = Console.ReadLine();
                        try
                        {
                            var iri = int.Parse(ri);
                            RegInfoTimerInterval = iri;
                            RegInfoTimer.Change(60000, RegInfoTimerInterval);
                            Console.WriteLine(">Name :\t\tRegInfoTimer\r\n>State :\t" + (RegInfoTimerInterval > 0) + "\r\n>Interval :\t" + RegInfoTimerInterval);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine(">无法识别输入内容");
                        }
                        break;
                    case "set SessionTimer":
                        Console.Write("Interval:");
                        var si = Console.ReadLine();
                        try
                        {
                            var isi = int.Parse(si);
                            SessionTimerInterval = isi;
                            SessionTimer.Change(60000, SessionTimerInterval);
                            Console.WriteLine(">Name :\t\tSessionTimer\r\n>State :\t" + (SessionTimerInterval > 0) + "\r\n>Interval :\t" + SessionTimerInterval);
                        }
                        catch
                        {
                            Console.WriteLine(">无法识别输入内容");
                        }
                        break;
                    case "Exit":
                        break;
                    default:
                        Console.WriteLine(">不能识别命令");
                        break;
                }
                Console.WriteLine();
            } while (input != "Exit");
        }
    }
}
